package cn.st4rlight.util.convert;

import org.apache.commons.lang3.StringUtils;

import cn.st4rlight.util.convert.base.IConverter;
import cn.st4rlight.util.exception.ConvertException;

/**
 * Boolean类型转换器
 *
 * @author st4rlight
 */
public class BooleanConverter implements IConverter<Boolean> {

    @Override
    public Boolean convert(String value) throws ConvertException {
        value = StringUtils.trimToEmpty(value).toLowerCase();
        if ("true".equalsIgnoreCase(value) || "1".equals(value)) {
            return Boolean.TRUE;
        } else if ("false".equalsIgnoreCase(value) || "0".equals(value)) {
            return Boolean.FALSE;
        } else {
            throw new ConvertException("无法将字符串\"" + value + "\"转换为Boolean类型");
        }
    }
}